package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入编号,编号作用如下:\n1:用户注册\n2:用户登录\n3:查询用户列表\n4:用户信息");
        System.out.print("请输入编号:");
        int choice = scan.nextInt();
        switch (choice) {
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误！");
        }

    }

    //处理用户注册
    public void userReg() {
        System.out.println("欢迎注册");
        User user = new User();
        System.out.println("请输入用户名：");
        Scanner scan = new Scanner(System.in);
        user.setUsername(scan.nextLine());
        System.out.println("请输入密码：");
        scan = new Scanner(System.in);
        user.setPassword(scan.nextLine());
        System.out.println("请输入昵称：");
        scan = new Scanner(System.in);
        user.setNickname(scan.nextLine());
        System.out.println("请输入年龄：");
        scan = new Scanner(System.in);
        user.setAge(scan.nextInt());
        System.out.println(user);
        //获取信息后进行验证,查看是否已存在,是否符合要求
        //验证通过,将信息插入数据库
        if (userMapper.selectByUsername(user.getUsername()) == null) {
            int result = userMapper.insert(user);
            System.out.println(result > 0 ? "注册成功" : "注册失败");
        } else {
            System.out.println("用户已存在");
        }
    }

    //处理用户登录
    public void userLogin() {
//        System.out.println("欢迎登录");
//        User user = new User();
//        Scanner scan = new Scanner(System.in);
//        System.out.println("请输入用户名:");
//        user.setUsername(scan.next());
//        System.out.println("请输入密码:");
//        user.setPassword(scan.next());
//        boolean flag = userMapper.logIn(user);
//        System.out.println(flag?"登录成功":"登录失败");


        System.out.println("欢迎登录");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scan.next();
        System.out.println("请输入密码:");
        String password = scan.next();
        //验证输入信息(验证用户名密码是否为空,或者输入不合规)
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                System.out.println("登录成功,欢迎" + user.getNickname() + "回来");
            } else {
                System.out.println("密码错误,登录失败");
            }
        } else {
            System.out.println("用户不存在");
        }


    }

    //处理用户列表
    public void userList() {
//        System.out.println("用户列表如下:");
//        List<String> list = userMapper.query();
//        for (String name : list) {
//            System.out.println(name);
//        }
        System.out.println("用户列表如下:");
        List<User> list = userMapper.selectAll();
        for (User user : list) {
            System.out.println(user.getId() + "\t " + user.getUsername() + "\t " + user.getNickname() + "\t " + user.getAge());
        }

    }

    //处理用户信息修改
    public void userUpdate() {
        System.out.println("修改用户信息");
        System.out.println("请输入需要修改用户:");
        Scanner scan = new Scanner(System.in);
        String username = scan.next();
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            System.out.println("请输入密码:");
            scan = new Scanner(System.in);
            String password = scan.next();
            if (user.getPassword().equals(password)) {
                User newUser = new User();
                newUser.setUsername(username);
                System.out.println("请输入修改后的密码:");
                scan = new Scanner(System.in);
                newUser.setPassword(scan.next());
                System.out.println("请输入修改后的昵称:");
                scan = new Scanner(System.in);
                newUser.setNickname(scan.next());
                System.out.println("请输入修改后的年龄:");
                scan = new Scanner(System.in);
                newUser.setAge(scan.nextInt());
                int result = userMapper.update(newUser);
                System.out.println(result > 0 ? "修改成功" : "修改失败,未知错误");
            } else {
                System.out.println("密码错误,请重新输入");
            }
        } else {
            System.out.println("用户不存在");
        }
    }
}
